cd "C:\Users\patri\OneDrive\Documents\2025\Glory Oscar"


use "stata_data.dta"

* -------------------------------
* AI_Policy_Exists
* -------------------------------
label define lbl_AIPolicy 0 "No" 1 "Currently in development" 2 "Yes"
label values AI_Policy_Exists lbl_AIPolicy

* -------------------------------
* AI_Interventions_Exist
* -------------------------------
label define lbl_AIInterv 0 "No" 1 "Yes"
label values AI_Interventions_Exist lbl_AIInterv

* -------------------------------
* Alt_Electricity_Use
* -------------------------------
label define lbl_AltElecUse 0 "No" 1 "Yes"
label values Alt_Electricity_Use lbl_AltElecUse

* -------------------------------
* AI_Innovation
* -------------------------------
label define lbl_AIInnov 1 "Very Weak" 2 "Weak" 3 "Moderate" 4 "Strong" 5 "Very Strong"
label values AI_Innovation lbl_AIInnov

* -------------------------------
* Internet_Affordability
* -------------------------------
label define lbl_IntAff 1 "Very Expensive" 2 "Expensive" 3 "Moderate" 4 "Affordable" 5 "Very Affordable"
label values Internet_Affordability lbl_IntAff

* Electricity_Affordability
label values Electricity_Affordability lbl_IntAff

* Alt_Electricity_Cost
label define lbl_AltElecCost 1 "Very Expensive" 2 "Expensive" 4 "Affordable"
label values Alt_Electricity_Cost lbl_AltElecCost

* Hardware_Cost
label values Hardware_Cost lbl_IntAff

* Cloud_Cost
label values Cloud_Cost lbl_IntAff

* -------------------------------
* Internet_Reliability
* -------------------------------
label define lbl_IntRel 1 "Very Unreliable" 2 "Unreliable" 3 "Moderate" 4 "Reliable" 5 "Very Reliable"
label values Internet_Reliability lbl_IntRel

* -------------------------------
* Electricity_Reliability
* -------------------------------
label define lbl_ElecRel 1 "Very Poor" 2 "Poor" 3 "Fair" 4 "Good" 5 "Excellent"
label values Electricity_Reliability lbl_ElecRel

* -------------------------------
* Hardware_Accessibility
* -------------------------------
label define lbl_HardAcc 1 "Not Accessible" 2 "Limited" 3 "Moderate" 4 "Wide" 5 "Easy"
label values Hardware_Accessibility lbl_HardAcc

* -------------------------------
* Cloud Access variables
* -------------------------------
label define lbl_CloudAcc 0 "Not Used" 1 "Rarely Used" 2 "Sometimes Used" 3 "Often Used" 4 "Primarily Source"
label values Access_Global_Cloud lbl_CloudAcc
label values Access_Local_Cloud lbl_CloudAcc
label values Access_Mixed_Cloud lbl_CloudAcc
label values Access_OnPrem lbl_CloudAcc

* -------------------------------
* Data_Accessibility
* -------------------------------
label define lbl_DataAcc 1 "Not Accessible" 2 "Difficult" 3 "Partially Accessible" 4 "Accessible" 5 "Easily Accessible"
label values Data_Accessibility lbl_DataAcc

* -------------------------------
* Data_Quality
* -------------------------------
label define lbl_DataQual 1 "Very Poor" 2 "Low" 3 "Moderate" 4 "Good" 5 "High"
label values Data_Quality lbl_DataQual

* -------------------------------
* Income_Group
* -------------------------------
label define lbl_IncomeGrp 1 "Low" 2 "Lower-middle" 3 "Upper-middle"
label values Income_Group lbl_IncomeGrp

* -------------------------------
* AI_Policy
* -------------------------------
gen AI_Policy = 0
replace AI_Policy = 1 if AI_Policy_Exists == 2

label define lbl_AIPolicy2 0 "No" 1 "Yes"
label values AI_Policy lbl_AIPolicy2


* -------------------------------
* Save
* -------------------------------
save "AI_Capacity.dta", replace

use "AI_Capacity.dta"


// Relationships
	

forvalues k = 1/3 {
	* -------------------------------
	* For AI Policy
	* -------------------------------
	
	local policy_name "policy_`k'.doc"
	
	if fileexists("`policy_name'"){
		erase "`policy_name'"
	}
		
	asdoc, save(`policy_name') row(Variable, Category, Yes(%), No(%), Chi2_P) ///
		title(AI Policy Table) font(Times New Roman) fs(12)
		
	foreach var of varlist Internet_Affordability Internet_Reliability Electricity_Affordability Electricity_Reliability ///
		Hardware_Accessibility Hardware_Cost Cloud_Cost Data_Accessibility Data_Quality {
		
		* Chi-squared test and p-value formatting
		quietly tab `var' AI_Policy  if Income_Group == `k', chi2
		local chi2 = round(r(chi2), 0.01)
		local p = r(p)

		if `p' <= 0.001 local star "***"
		else if `p' <= 0.01 local star "**"
		else if `p' <= 0.05 local star "*"
		else local star ""

		local chi2star = "`chi2'`star'"
		local chi2_fmt : display %5.1f `chi2'
		local p_fmt : display %4.3f `p'
		local chi2p = "`chi2_fmt'(`p_fmt')" + " " 

		* Loop through each level of the variable
		quietly levelsof `var', local(levels)

		foreach lvl of local levels {
			capture confirm string variable `var'
			if !_rc {
				* For string variables
				quietly count if `var' == "`lvl'" & !missing(AI_Policy) & Income_Group == `k'
				local group_total = r(N)
				if `group_total' == 0 continue

				quietly count if `var' == "`lvl'" & AI_Policy == 1 & Income_Group == `k'
				local ai_pct = round((r(N)/`group_total')*100, 0.01)

				quietly count if `var' == "`lvl'" & AI_Policy == 0 & Income_Group == `k'
				local nonai_pct = round((r(N)/`group_total')*100, 0.01)
			}
			else {
				* For numeric variables
				quietly count if `var' == `lvl' & !missing(AI_Policy) & Income_Group == `k'
				local group_total = r(N)
				if `group_total' == 0 continue

				quietly count if `var' == `lvl' & AI_Policy == 1 & Income_Group == `k'
				local ai_pct = round((r(N)/`group_total')*100, 0.01)

				quietly count if `var' == `lvl' & AI_Policy == 0 & Income_Group == `k'
				local nonai_pct = round((r(N)/`group_total')*100, 0.01)
			}

			* Get value label if available
			local val_lbl : value label `var'
			if "`val_lbl'" != "" {
				local lbl : label `val_lbl' `lvl'
				if "`lbl'" == "" local lbl "`lvl'"
			} 
			else local lbl "`lvl'"
			
			* Format percentages to 2 decimal places
			local ai_pct_fmt : display %5.2f `ai_pct'
			local nonai_pct_fmt : display %5.2f `nonai_pct'
			
			* Export to asdoc (now correctly formatted)
			asdoc, row(`var', `lbl', `ai_pct_fmt', `nonai_pct_fmt', `p_fmt') ///
				append font(Times New Roman) fs(12) dec(2)
		}
	}


	* -------------------------------
	* For AI Intervention
	* -------------------------------
	local intervention_name "intervention_`k'.doc"
	
	if fileexists("`intervention_name'"){
		erase "`intervention_name'"
	}
		
	asdoc, save(`intervention_name') row(Variable, Category, Yes(%), No(%), Chi2_P) ///
		title(AI Intervantion Table) font(Times New Roman) fs(12)
		
	foreach var of varlist Internet_Affordability Internet_Reliability Electricity_Affordability Electricity_Reliability ///
		Hardware_Accessibility Hardware_Cost Cloud_Cost Data_Accessibility Data_Quality {
		
		* Chi-squared test and p-value formatting
		quietly tab `var' AI_Interventions_Exist if Income_Group == `k', chi2
		local chi2 = round(r(chi2), 0.01)
		local p = r(p)

		if `p' <= 0.001 local star "***"
		else if `p' <= 0.01 local star "**"
		else if `p' <= 0.05 local star "*"
		else local star ""

		local chi2star = "`chi2'`star'"
		local chi2_fmt : display %5.1f `chi2'
		local p_fmt : display %4.3f `p'
		local chi2p = "`chi2_fmt'(`p_fmt')" + " " 

		* Loop through each level of the variable
		quietly levelsof `var', local(levels)

		foreach lvl of local levels {
			capture confirm string variable `var'
			if !_rc {
				* For string variables
				quietly count if `var' == "`lvl'" & !missing(AI_Interventions_Exist) & Income_Group == `k'
				local group_total = r(N)
				if `group_total' == 0 continue

				quietly count if `var' == "`lvl'" & AI_Interventions_Exist == 1 & Income_Group == `k'
				local ai_pct = round((r(N)/`group_total')*100, 0.01)

				quietly count if `var' == "`lvl'" & AI_Interventions_Exist == 0 & Income_Group == `k'
				local nonai_pct = round((r(N)/`group_total')*100, 0.01)
			}
			else {
				* For numeric variables
				quietly count if `var' == `lvl' & !missing(AI_Interventions_Exist) & Income_Group == `k'
				local group_total = r(N)
				if `group_total' == 0 continue

				quietly count if `var' == `lvl' & AI_Interventions_Exist == 1 & Income_Group == `k'
				local ai_pct = round((r(N)/`group_total')*100, 0.01)

				quietly count if `var' == `lvl' & AI_Interventions_Exist == 0 & Income_Group == `k'
				local nonai_pct = round((r(N)/`group_total')*100, 0.01)
			}

			* Get value label if available
			local val_lbl : value label `var'
			if "`val_lbl'" != "" {
				local lbl : label `val_lbl' `lvl'
				if "`lbl'" == "" local lbl "`lvl'"
			} 
			else local lbl "`lvl'"
			
			* Format percentages to 2 decimal places
			local ai_pct_fmt : display %5.2f `ai_pct'
			local nonai_pct_fmt : display %5.2f `nonai_pct'
			
			* Export to asdoc (now correctly formatted)
			asdoc, row(`var', `lbl', `ai_pct_fmt', `nonai_pct_fmt', `p_fmt') ///
				append font(Times New Roman) fs(12) dec(2)
		}
	}

	* -------------------------------
	* For AI Innovation
	* -------------------------------
	local innovation_name "innovation_`k'.doc"
	
	if fileexists("`innovation_name'"){
		erase "`innovation_name'"
	}

	* Create table with AI_Innovation as the main column
	asdoc, save(`innovation_name') row(Variable, Category, VeryWeak(%), Weak(%), Moderate(%), Strong(%), VeryStrong(%), Chi2_P) ///
		title(AI Innovation Table) font(Times New Roman) fs(12)

	foreach var of varlist Internet_Affordability Internet_Reliability Electricity_Affordability Electricity_Reliability ///
		Hardware_Accessibility Hardware_Cost Cloud_Cost Data_Accessibility Data_Quality {

		* Run chi-square test
		quietly tab `var' AI_Innovation if Income_Group == `k' , chi2
		local chi2 = round(r(chi2), 0.01)
		local p = r(p)

		* Significance stars
		if `p' <= 0.001 local star "***"
		else if `p' <= 0.01 local star "**"
		else if `p' <= 0.05 local star "*"
		else local star ""

		local chi2star = "`chi2'`star'"
		local chi2_fmt : display %5.1f `chi2'
		local p_fmt : display %4.3f `p'
		local chi2p = "`chi2_fmt'(`p_fmt')" + " "

		* Get levels of the variable
		quietly levelsof `var', local(levels)

		foreach lvl of local levels {
			capture confirm string variable `var'
			if !_rc {
				* For string variables
				quietly count if `var' == "`lvl'" & !missing(AI_Innovation) & Income_Group == `k'
				local group_total = r(N)
				if `group_total' == 0 continue

				* Calculate percentages for each AI_Innovation category (1-5)
				forvalues ai = 1/5 {
					quietly count if `var' == "`lvl'" & AI_Innovation == `ai'
					local pct`ai' = round((r(N)/`group_total')*100, 0.01)
				}
			}
			else {
				* For numeric variables
				quietly count if `var' == `lvl' & !missing(AI_Innovation) & Income_Group == `k'
				local group_total = r(N)
				if `group_total' == 0 continue

				forvalues ai = 1/5 {
					quietly count if `var' == `lvl' & AI_Innovation == `ai' & Income_Group == `k'  
					local pct`ai' = round((r(N)/`group_total')*100, 0.01)
				}
			}

			* Get value labels
			local val_lbl : value label `var'
			if "`val_lbl'" != "" {
				local lbl : label `val_lbl' `lvl'
				if "`lbl'" == "" local lbl "`lvl'"
			} 
			else local lbl "`lvl'"
			
			* Format all percentages to 2 decimal places
			forvalues ai = 1/5 {
				local pct`ai'_fmt : display %5.2f `pct`ai''
			}

			* Write row to asdoc
			asdoc, row(`var', `lbl', `pct1_fmt', `pct2_fmt', `pct3_fmt', `pct4_fmt', `pct5_fmt', `p_fmt') ///
				append font(Times New Roman) fs(12) dec(2)
		}
	}
}


*---------------------------
* 2 categories of innovation
*---------------------------

recode AI_Innovation (1/2=1 "Weak") (3/5=2 "moderate or Strong"), gen(ai_innovation) label(innovaton_lbl)

label variable ai_innovation "AI Innovation"

forvalues k = 1/3 {

	local innovation_name "innovation_3cat_`k'.doc"

	if fileexists("`innovation_name'"){
			erase "`innovation_name'"
	}

	* Create table with AI_Innovation as the main column
	asdoc, save(`innovation_name') row(Variable, Category, Weak(%), Moderate_Strong(%) Chi2_P) ///
		title(AI Innovation Table) font(Times New Roman) fs(12)

	foreach var of varlist Internet_Affordability Internet_Reliability Electricity_Affordability Electricity_Reliability ///
		Hardware_Accessibility Hardware_Cost Cloud_Cost Data_Accessibility Data_Quality {

		* Run chi-square test
		quietly tab `var' ai_innovation if Income_Group == `k' , chi2
		local chi2 = round(r(chi2), 0.01)
		local p = r(p)

		* Significance stars
		if `p' <= 0.001 local star "***"
		else if `p' <= 0.01 local star "**"
		else if `p' <= 0.05 local star "*"
		else local star ""

		local chi2star = "`chi2'`star'"
		local chi2_fmt : display %5.1f `chi2'
		local p_fmt : display %4.3f `p'
		local chi2p = "`chi2_fmt'(`p_fmt')" + " "

		* Get levels of the variable
		quietly levelsof `var', local(levels)

		foreach lvl of local levels {
			capture confirm string variable `var'
			if !_rc {
				* For string variables
				quietly count if `var' == "`lvl'" & !missing(ai_innovation) & Income_Group == `k'
				local group_total = r(N)
				if `group_total' == 0 continue

				* Calculate percentages for each ai_innovation category (1-5)
				forvalues ai = 1/2 {
					quietly count if `var' == "`lvl'" & ai_innovation == `ai'
					local pct`ai' = round((r(N)/`group_total')*100, 0.01)
				}
			}
			else {
				* For numeric variables
				quietly count if `var' == `lvl' & !missing(ai_innovation) & Income_Group == `k'
				local group_total = r(N)
				if `group_total' == 0 continue

				forvalues ai = 1/2 {
					quietly count if `var' == `lvl' & ai_innovation == `ai' & Income_Group == `k'  
					local pct`ai' = round((r(N)/`group_total')*100, 0.01)
				}
			}

			* Get value labels
			local val_lbl : value label `var'
			if "`val_lbl'" != "" {
				local lbl : label `val_lbl' `lvl'
				if "`lbl'" == "" local lbl "`lvl'"
			} 
			else local lbl "`lvl'"
			
			* Format all percentages to 2 decimal places
			forvalues ai = 1/2 {
				local pct`ai'_fmt : display %5.2f `pct`ai''
			}

			* Write row to asdoc
			asdoc, row(`var', `lbl', `pct1_fmt', `pct2_fmt', `p_fmt') ///
				append font(Times New Roman) fs(12) dec(2)
		}
	}
}
